﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using SeleniumToolkit.Util;

namespace SeleniumToolkit.Install
{
    public class NUnitVersion
    {
        private static Dictionary<string,NUnitVersion> _versions;
        private RegistryKeyHelper _registry;
        private string _addinFolder;


        #region Static Members
        static NUnitVersion()
        {
            _versions = new Dictionary<string, NUnitVersion>();

            _versions.Add("2.4.8", new NUnitVersion(@"SOFTWARE\Nunit.org\NUnit\2.4.8", @"bin\addins"));
            _versions.Add("2.5.2", new NUnitVersion(@"SOFTWARE\Nunit.org\NUnit\2.5.2", @"bin\net-2.0\addins"));
        }

        public static Dictionary<string,NUnitVersion> Versions
        {
            get
            {
                return _versions;
            }
        }
        #endregion

        public NUnitVersion(string registryKey, string relativeAddinPath)
        {
            _registry = new RegistryKeyHelper(registryKey);
            RegistryKey = registryKey;
            RelativeAddinPath = relativeAddinPath;
        }

        /// <summary>
        /// Gets the full path to the addin folder for this NUnit Version.
        /// </summary>
        /// <remarks>The folder will be created if it is not present.</remarks>
        public string AddinFolder
        {
            get
            {
                if (_addinFolder == null)
                {
                    try
                    {
                        // if the addin is installed, then get the install directory
                        if (Installed)
                        {
                            string installDir = _registry.GetValue("InstallDir");
                            if (installDir != null)
                            {
                                // get the path to the addin folder and create it if it doesn't exist
                                string addinFolder = Path.Combine(installDir, RelativeAddinPath);
                                if (!Directory.Exists(addinFolder))
                                {
                                    Directory.CreateDirectory(addinFolder);
                                }

                                _addinFolder = addinFolder;
                            }
                            else
                            {
                                _addinFolder = String.Empty;
                            }
                        }
                        else
                        {
                            _addinFolder = String.Empty;
                        }
                    }
                    catch
                    {
                        _addinFolder = String.Empty;
                    }
                }
                return _addinFolder;
            }
        }

        /// <summary>
        /// Gets the Registry Key for this NUnit version
        /// </summary>
        public string RegistryKey
        {
            get;
            protected set;
        }

        /// <summary>
        /// Gets the path of the addin folder relative to the install folder
        /// </summary>
        public string RelativeAddinPath
        {
            get;
            protected set;
        }

        /// <summary>
        /// Returns true when the Registry Key for this NUnit version is present
        /// </summary>
        public bool Installed
        {
            get
            {
                return _registry.RegistryKeyExists;
            }
        }
    }
}
